<template>
  <div class="app-container">
    <el-row>
  <el-col :span="18">
    
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>余额</span>
            <span>{{accountData.cash}}</span>
          </div>
          <div>
      <el-button slot="reference">账单</el-button>
      <el-button slot="reference">存款</el-button>
      <el-button slot="reference">取款</el-button>
      <el-button slot="reference">收入</el-button>
      <el-button slot="reference">支出</el-button>
      </div>
         </el-card>
         <el-row>
            <el-col :span="12">
              
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>本月支出</span>
            <el-button style="float: right; padding: 3px 0" type="text" >更多</el-button>
          </div>
          <div>
      </div>
         </el-card>
              
            </el-col>
            <el-col :span="12">
              
              
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>近1周消费</span>
          </div>
          <div>
      </div>
         </el-card>

            </el-col>
          </el-row>
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>资金走势</span>
          </div>
         <el-table
          :data="accountList"
          stripe
          style="width: 100%"
          height="400">
          <el-table-column
            prop="cycle"
            label="日期"
            width="180">
          </el-table-column>
          <el-table-column
            prop="amount"
            label="资产"
            width="100">
          </el-table-column>
          <el-table-column
            prop="profit"
            width="200"
            label="盈利">
          </el-table-column>
          <el-table-column
            prop="loss"
            width="200"
            label="亏损">
          </el-table-column>
          <el-table-column
            prop="cash"
            width="200"
            label="现金">
          </el-table-column>
          <el-table-column
            prop="updatedAt"
            width="180"
            label="发生时间">
          </el-table-column>
        </el-table>
         </el-card>

  </el-col>
  <el-col :span="6">
    
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>流水</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="handleTurnList">更多</el-button>
          </div>
         <el-table
          :data="authorizeList"
          stripe
          style="width: 100%"
          height="400">
          <el-table-column
            prop="symbol"
            label="科目"
            width="180">
            <template slot-scope="{row}">
              <span>{{ row.subject.title }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="amount"
            label="金额"
            width="100">
          </el-table-column>
          <el-table-column
            prop="balance"
            width="200"
            label="余额">
          </el-table-column>
          <el-table-column
            prop="createdAt"
            width="180"
            label="发生时间">
          </el-table-column>
        </el-table>
         </el-card>
        <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>其他</span>
            <el-button style="float: right; padding: 3px 0" type="text" >更多</el-button>
          </div>
         </el-card>

  </el-col>
</el-row>
    <div class="filter-container">
    </div>
    <div class="filter-container" style="display:none;">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="合约" prop="symbol">
          <el-input v-model="filterForm.symbol" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="开仓时间" prop="tradeTime">
          <el-date-picker 
          clearable
            v-model="filterForm.tradeTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账户" prop="invertID">
          <el-input v-model="filterForm.invertID" clearable placeholder="客户ID/客户电话/客户姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      accountData:{},
      accountList:[],
      authorizeList: [],
      windowHeight:"800",
      total: 0,
      listLoading: false,
      connected:"loading",
      filterForm: {
        page: 1,
        size: 10,
        accountID: 0,
        sort: '+id'
      }
    }
  },
  created() {
    this.init()
  },
  destroyed(){
    this.close()
  },
  methods: {
    init: function () {
      if(typeof(WebSocket) === "undefined"){
          alert("您的浏览器不支持socket")
      }else{
          // 实例化socket
          this.socket = new WebSocket(getHost())
          // 监听socket连接
          this.socket.onopen = this.open
          // 监听socket错误信息
          this.socket.onerror = this.error
          // 监听socket消息
          this.socket.onmessage = this.getMessage
      }
    },
    open: function () {

        this.connected = 'loaded'
        let _this = this;
        this.getList()
        setTimeout(function(){
          _this.getAccountDetail()
        },1000)
        setTimeout(function(){
          _this.getAccountAnalysis()
        },1500)
    },
    error: function (e) {
        console.log("连接错误",JSON.stringify(e))
    },
    getMessage: function (receiveData) {
      receiveData = JSON.parse(receiveData.data)
        console.log(receiveData)
      if(receiveData.status != 200){
          console.log(receiveData.msg)
      }else{
        if(receiveData.conversationType == "finance.account.analysis"){
          this.accountList = receiveData.data.list

        }
        if(receiveData.conversationType == "finance.account.detail"){
          this.accountData = receiveData.data

        }
        if(receiveData.conversationType == "finance.turnover.list"){
          this.authorizeList = receiveData.data.list

        }
      }
    },
    send: function (params) {
      params = JSON.stringify(params)
      this.socket.send(params)
    },
    close: function () {
        console.log("socket已经关闭")
    },
    destroyed () {
        // 销毁监听
        this.socket.onclose = this.close
    },
    getList:function(){
      this.send({
        "conversationType":"finance.account.detail",
        "message":{"accountID":this.filterForm.accountID},
        "target":{"id":"3"}
      })
    },
    getAccountDetail:function(){
      this.send({
        "conversationType":"finance.turnover.list",
        "message":{"page":this.filterForm.page,"size":this.filterForm.size},
        "target":{"id":"3"}
      })
    },
    getAccountAnalysis:function(){
      this.send({
        "conversationType":"finance.account.analysis",
        "message":{"page":this.filterForm.page,"size":this.filterForm.size},
        "target":{"id":"3"}
      })
    },
    handleTurnList() {
    },
    sortChange:function(){

    }
  }
}
</script>
<style>

</style>